//
//  LBNewCategoryMainView.m
//  AFNetworking
//
//  Created by luckyRoy on 2018/3/15.
//

#import "LBNewCategoryMainView.h"
#import "LBCategorySettingModel.h"
#import "LBCategoryMainView.h"
#import "LBLoadingView.h"
#import "LBCategoryManager.h"
#import "Masonry.h"
#import "LBAlert.h"
#import "UIApplication+LBViewController.h"

@interface LBNewCategoryMainView ()

@property (nonatomic, strong) LBCategorySettingModel *settingModel;

@property (nonatomic, copy) LBNewCategoryMainViewChose choseBlock;

@property (nonatomic, strong) NSArray <NSString *>*codeArray;

@property (nonatomic, strong) LBCategoryMainView *mainView;

@property (nonatomic, assign) LBCategoryBizCodeType requestCodeType;

@end

@implementation LBNewCategoryMainView

- (instancetype)initWithFrame:(CGRect)frame
             isSingleSelected:(BOOL)isSingleSelected
                    CodeArray:(NSArray <NSString *> *)selectedCodeArray
                     codeType:(LBCategoryBizCodeType)codeType
             maxSelectedLevel:(NSInteger)level
                     complete:(LBNewCategoryMainViewChose)complete
{
    self = [super initWithFrame:frame];
    if (self) {
        //获取主分类所在展示vc
        self.settingModel.isSingleSelected = isSingleSelected;
        self.requestCodeType = codeType;
        self.settingModel.maxLevel = level;
        self.choseBlock = complete;
        self.codeArray = selectedCodeArray;
        self.settingModel.codeType = codeType;
        
        if (LBCategoryBizCodeTypePublicRequirement == codeType) {
            self.requestCodeType = LBCategoryBizCodeTypeDemand;
        } else if (LBCategoryBizCodeTypeEnterResource == codeType) {
            self.requestCodeType = LBCategoryBizCodeTypeCompany;
        }
        
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self addSubview:self.mainView];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self requestForListModel];
}

#pragma mark - request

- (void)requestForListModel
{
    __weak __typeof(&*self)weakSelf = self;
    [LBLoadingView showInView:self];
    if (LBCategoryBizCodeTypeBookingRequirement == self.requestCodeType ||
        LBCategoryBizCodeTypeBookingResource == self.requestCodeType) {
        [LBCategoryManager getBookingCategoryListByBizType:self.requestCodeType Success:^(NSArray<LBCategoryModel *> *unSelctedArray, NSArray<LBCategoryModel *> *selctedArray) {
            [LBLoadingView dismiss];
            weakSelf.settingModel.selectedModelArray = [NSMutableArray arrayWithArray:selctedArray];
            [weakSelf.settingModel setOriginalArrayByData:[weakSelf.settingModel getOriginalArrayByOriginalData:unSelctedArray]];
            [weakSelf reloadListView];
        } fail:^(NSString *errMsg) {
            [LBLoadingView dismiss];
            [LBAlert say:errMsg];
        }];
    } else {
        [LBCategoryManager getCategoryListByBizCode:self.requestCodeType Depth:@(self.settingModel.maxLevel) Success:^(NSArray<LBCategoryModel *> *modelArray) {
            [LBLoadingView dismiss];
            weakSelf.settingModel.selectedModelArray = [LBCategorySettingModel selectedModelArrayFromCodeArray:weakSelf.codeArray totalArray:modelArray isSingleSelected:weakSelf.settingModel.isSingleSelected];
            [weakSelf.settingModel setOriginalArrayByData:[weakSelf.settingModel getOriginalArrayByOriginalData:modelArray]];
            [weakSelf reloadListView];
        } fail:^(NSString *errMsg) {
            [LBLoadingView dismiss];
            [LBAlert say:errMsg];
        }];
    }
    
}

- (void)reloadListView
{
    [self.mainView reloadViewByNewListModels:[LBCategorySettingModel getListModelBySettingModel:self.settingModel currentPage:LBCategorySettingPageTypeHomePage]];
}

#pragma mark - getter

- (LBCategorySettingModel *)settingModel
{
    if (!_settingModel) {
        _settingModel = [[LBCategorySettingModel alloc]init];
    }
    return _settingModel;
}

- (LBCategoryMainView *)mainView
{
    if (!_mainView) {
        LBCategoryMainViewLayout *viewLayout = [LBCategoryMainViewLayout new];
        viewLayout.isShowTopSelectedView = [self.settingModel isShowTopSelectedView];
        if (LBCategoryBizCodeTypeBookingRequirement ==self.settingModel.codeType ||
            LBCategoryBizCodeTypeBookingResource ==self.settingModel.codeType) {
            //订阅 展示搜索 顶部选择
            viewLayout.isShowSearchView = YES;
        } else if (LBCategoryBizCodeTypeEnterResource ==self.settingModel.codeType ||
                   LBCategoryBizCodeTypePublicRequirement ==self.settingModel.codeType) {
            //入驻 展示搜索 底部选择
            viewLayout.isShowSearchView = YES;
            viewLayout.isShowBottomSelectedView = !self.settingModel.isSingleSelected;
        } else {
            //其他 仅搜索
            viewLayout.isShowSearchView = YES;
            viewLayout.isShowBottomSelectedView = !self.settingModel.isSingleSelected;
        }
        __weak __typeof(&*self)weakSelf = self;
        _mainView = [[LBCategoryMainView alloc]initWithFrame:self.bounds settingModel:self.settingModel viewLayout:viewLayout complete:^(LBCategoryMainViewLayout *blockModel,NSMutableArray *selectedArray) {
            id parm = nil;
            if (blockModel.isLookingAll) {
                //查看全部
                parm = @(YES);
            }
            if (weakSelf.choseBlock) {
                weakSelf.choseBlock(selectedArray, parm);
            }
        }];
    }
    return _mainView;
}

@end
